There is a problem with dplyr and plotly. When loaded tofether both have functions of same name so must point to the library you reall need, i.e. dplyr::select()
#library(reticulate)
library(tidyverse)
library(plotly)
library(janitor)
library(readxl)
player_projection <- read_csv("DFF_NHL_cheatsheet.csv", col_names = TRUE)
slice takes the top 3 grouped records
later I ungroup and combine field names.
Then I filter out points above 60 to create a new object for graphing
team__reg_line <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(!position == "G") %>%
filter(!reg_line == 0) %>%
select(team, reg_line, l5_ppg_max, salary) %>%
arrange(team, reg_line, desc(l5_ppg_max)) %>%
group_by(team, reg_line) %>%
dplyr::slice(1:3) %>%
dplyr::summarise(total_pts = sum(l5_ppg_max), salary = sum(salary)) %>%
dplyr::ungroup() %>%
unite(team_line, team, reg_line, sep = "_", remove = TRUE)
## `summarise()` has grouped output by 'team'. You can override using the
## `.groups` argument.
team_pts_max_20 <- team__reg_line %>%
dplyr::filter(total_pts >= 60)
this is called a lollypop chart
# install.packages("ggplot2")
library(ggplot2)
ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
## Use plotly with ggplot2
p <- ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = round(total_pts)), color = "white", size = 3)
ggplotly(p)
Review choices for goalie bases on DK Spreadsheet
Filter for Goalies only
Reduce data elements
sort descending value
create a variance/spread column replacing l5_
create new field names for connecting to other databases
goalies <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "G") %>%
select(1:3, 6, 8, 10:16, 18:20) %>%
arrange(desc(value_projection), desc(l5_ppg_max), desc(salary)) %>%
mutate(variance = l5_ppg_max - l5_ppg_floor) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
goalie_select_10 <- goalies %>%
head(10)
# install.packages("ggplot2")
library(ggplot2)
ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
p <- ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 5000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
ggplotly(p)
centers <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "C") %>%
select(1:3, 6, 8, 10:16, 18:20) %>%
arrange(desc(value_projection), desc(l5_ppg_max), desc(salary)) %>%
mutate(variance = l5_ppg_max - l5_ppg_floor) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
center_select_15 <- centers %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(center_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
p <- ggplot(center_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
ggplotly(p)
wings <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "W") %>%
select(1:3, 6, 8, 10:16, 18:20) %>%
arrange(desc(value_projection), desc(l5_ppg_max), desc(salary)) %>%
mutate(variance = l5_ppg_max - l5_ppg_floor) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
wing_select_15 <- wings %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(wing_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
## Create the wing Selection chart in plotly
p <- ggplot(wing_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
ggplotly(p)
defense <- player_projection %>%
clean_names() %>%
mutate_if(is.numeric, ~replace_na(., 0)) %>%
filter(position == "D") %>%
select(1:3, 6, 8, 10:16, 18:20) %>%
arrange(desc(value_projection), desc(l5_ppg_max), desc(salary)) %>%
mutate(variance = l5_ppg_max - l5_ppg_floor) %>%
unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
unite(name_team, last_name, team, sep = "_", remove = FALSE)
defense_select_15 <- defense %>%
head(15)
# install.packages("ggplot2")
library(ggplot2)
ggplot(defense_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
## Create the wing Selection chart in plotly
p <- ggplot(defense_select_15, aes(x = name_team, y = salary)) +
geom_segment(aes(x = reorder(name_team, value_projection), xend = name_team, y = 2000, yend = salary)) +
geom_point() +
coord_flip() +
geom_point(size = 12, pch = 21, bg = 4, col = 1) +
geom_text(aes(label = value_projection), color = "white", size = 3)
ggplotly(p)